package GUI.List;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.*;

import GUI.TabPanel.DataBasePanel;
import Struct.Tables;

public class TableList extends JScrollPane {

	private static final long serialVersionUID = 1L;
	private JList<String> List = new JList<String>();
	private DefaultListModel<String> Model = new DefaultListModel<String>();
	private DataBasePanel DataBase;
	
	public void reFresh(ArrayList<Tables> TableList) {
		Model.clear();
		for (Tables table : TableList) {
			Model.addElement(table.name);
		}
		List.setModel(Model);
	}
	
	@SuppressWarnings("unchecked")
	private void initList() {
		List.setModel(Model);
		List.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					TwoClick(List.getSelectedValue());
				}
			}
		});
	}
	
	private void TwoClick(Object SelectObject) {
		if(SelectObject != null) {
			this.DataBase.Refresh(SelectObject);
		}
	}
	
	public void setDataBase(DataBasePanel DataBase) {
		this.DataBase = DataBase;
	}

	public TableList() {
		initList();
		this.setViewportView(List);
	}
}